package com.black.knight.chess.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.media.MediaPlayer;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.black.knight.chess.AddPGNGameActivity;
import com.black.knight.chess.R;
import com.black.knight.chess.SahMatActivity;
import com.black.knight.chess.SettingsActivity;
import com.black.knight.chess.StartPositionActivity;
import com.black.knight.chess.domain.DeviceItem;
import com.black.knight.chess.domain.Game;
import com.black.knight.chess.domain.Move;
import com.black.knight.chess.enums.NewGame;
import com.black.knight.chess.model.SahModel;
import com.black.knight.chess.model.SettingsModel;
import com.black.knight.chess.plugins.BluetoothPlugin;

public class Utils {
    public static Toast toast;
    
    public static ExecutorService executorService = Executors.newCachedThreadPool();
    
    private static MediaPlayer mediaPlayerMove;
    private static MediaPlayer mediaPlayerCastle;
    private static MediaPlayer mediaPlayerCapture;
    private static MediaPlayer mediaPlayerCheck;
    private static MediaPlayer mediaPlayerCheckmate;
    private static MediaPlayer mediaPlayerPlayer1Wins;
    private static MediaPlayer mediaPlayerPlayer2Wins;
    
    public static void displayToastMessage(Context context, String message) {		
    	LayoutInflater inflater = SahModel.context.getLayoutInflater();
		View layout = inflater.inflate(R.layout.toast, (ViewGroup) SahModel.context.findViewById(R.id.toast_layout_root));

		TextView text = (TextView) layout.findViewById(R.id.text);
		text.setText(message);
    	
    	if(toast != null) toast.cancel();
    	toast = new Toast(SahModel.context.getApplicationContext());
    	toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
		toast.setDuration(Toast.LENGTH_SHORT);
		toast.setView(layout);
		toast.show();
    }
    
    public static ProgressBar computerThinking(SahMatActivity context, String message) {		
    	final ProgressBar progressBarAndroidThinking = (ProgressBar) context.findViewById(R.id.progressBarAndroidThinking);
    	
    	if(progressBarAndroidThinking != null) {
    		progressBarAndroidThinking.setVisibility(ProgressBar.VISIBLE);
    		
    		context.requestAd();
    	}
    	
    	return progressBarAndroidThinking;
    	/*CustomizeLoadingDialog dialog = new CustomizeLoadingDialog(context, message);

    	try {
    		dialog.show();
    	} catch (Exception e) { }
    	
    	return dialog;*/
    }
    
    public static void displayGameFinishedMessage(Context context, String message) {
    	TextView myMsg = new TextView(context);
    	myMsg.setText(message);
    	myMsg.setTextSize(18);
    	myMsg.setTextColor(context.getResources().getColor(R.color.white));
    	myMsg.setPadding(10, 20, 10, 20);
    	myMsg.setGravity(Gravity.CENTER_HORIZONTAL);
    	
    	AlertDialog.Builder builder = new AlertDialog.Builder(context);
    	builder.setView(myMsg)
    	       .setPositiveButton(context.getResources().getString(R.string.new_game), new DialogInterface.OnClickListener() {
    	           public void onClick(DialogInterface dialog, int id) {
    	        	   Utils.chooseNewGameType(SahModel.context);
    	           }
    	       }).setNeutralButton(context.getResources().getString(R.string.save_game), new DialogInterface.OnClickListener() {
    	           public void onClick(DialogInterface dialog, int id) {
    	        	   Intent intent2 = new Intent();
    		           intent2.setClass(SahModel.context, AddPGNGameActivity.class);
    		           SahModel.context.startActivity(intent2);
    	           }
    	       }).setNegativeButton(context.getResources().getString(R.string.close), new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		           }
		       });
    	builder.create().show();
    }

    
    public static void displayAlertMessage(Context context, String message) {
    	TextView myMsg = new TextView(context);
    	myMsg.setText(message);
    	myMsg.setTextSize(18);
    	myMsg.setTextColor(context.getResources().getColor(R.color.white));
    	myMsg.setPadding(10, 20, 10, 20);
    	myMsg.setGravity(Gravity.CENTER_HORIZONTAL);
    	
    	AlertDialog.Builder builder = new AlertDialog.Builder(context);
    	builder.setView(myMsg)
    	       .setPositiveButton(context.getResources().getString(R.string.ok), new DialogInterface.OnClickListener() {
    	           public void onClick(DialogInterface dialog, int id) {
    	           }
    	       });
    	builder.create().show(); 	
    }
    
    public static Date toUTC(Date date) {
    	return new Date(Date.UTC(date.getYear(), date.getMonth(), date.getDay(), date.getHours(), date.getMinutes(), date.getSeconds()));
    }
    
    public static void displayAbout(Context context) {
    	String version = "";
		try {
			version = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
		} catch (NameNotFoundException e) {
		}
    	
    	AlertDialog.Builder builder = new AlertDialog.Builder(context);
    	builder.setMessage(context.getResources().getString(R.string.about_content))
    	       .setTitle(context.getResources().getString(R.string.app_name) + " " + version)
    	       .setIcon(R.drawable.icon)
    	       .setPositiveButton(context.getResources().getString(R.string.ok), new DialogInterface.OnClickListener() {
    	           public void onClick(DialogInterface dialog, int id) {
    	           }
    	       });
    	builder.create().show();
    }
    
    public static void displayWelcome(Context context) {
    	AlertDialog.Builder builder = new AlertDialog.Builder(context);
    	builder.setMessage(context.getResources().getString(R.string.welcome_message))
    	       .setTitle(context.getResources().getString(R.string.app_name))
    	       .setIcon(R.drawable.icon)
    	       .setPositiveButton(context.getResources().getString(R.string.ok), new DialogInterface.OnClickListener() {
    	           public void onClick(DialogInterface dialog, int id) {
    	           }
    	       });
    	builder.create().show();
    }
    
    public static void chooseNewGameType(final SahMatActivity activity) {
    	
    	SahModel.getInstance().reset(null);
    	BluetoothPlugin.getInstance().stop();
    	SahModel.getInstance().getBlackClock().stop();
    	SahModel.getInstance().getWhiteClock().stop();
    	
    	final ArrayList<DeviceItem> gameOptions = new ArrayList<DeviceItem>();
    	
    	gameOptions.add(new DeviceItem(activity.getResources().getString(R.string.human_device), R.drawable.human));
    	gameOptions.add(new DeviceItem(activity.getResources().getString(R.string.computer_device_medium), R.drawable.android));
    	gameOptions.add(new DeviceItem(activity.getResources().getString(R.string.computer_device_hard), R.drawable.android));
    	
    	if(BluetoothPlugin.getInstance().getmBluetoothAdapter() != null) {
    		gameOptions.add(new DeviceItem(activity.getResources().getString(R.string.bluetooth_host), R.drawable.bluetooth));
    		gameOptions.add(new DeviceItem(activity.getResources().getString(R.string.bluetooth_guest), R.drawable.bluetooth));
    	}
    	
    	gameOptions.add(new DeviceItem(activity.getResources().getString(R.string.set_start_position), R.drawable.startposition));

		ListAdapter adapter = new ArrayAdapter<DeviceItem>(activity, android.R.layout.select_dialog_item, android.R.id.text1, gameOptions){
	        public View getView(int position, View convertView, ViewGroup parent) {
	            View v = super.getView(position, convertView, parent);
	            TextView tv = (TextView)v.findViewById(android.R.id.text1);

	            tv.setCompoundDrawablesWithIntrinsicBounds(gameOptions.get(position).icon, 0, 0, 0);
	            tv.setTextSize(18);

	            int dp5 = (int) (5 * activity.getResources().getDisplayMetrics().density + 0.5f);
	            tv.setCompoundDrawablePadding(dp5);

	            return v;
	        }
		};

    	AlertDialog.Builder builder = new AlertDialog.Builder(activity);
    	builder.setTitle(activity.getResources().getString(R.string.play_new_game_against) + ":");
    	builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
    	    public void onClick(DialogInterface dialog, int item) {
    	    	if(item == 0) {
    	    		/*if(SettingsModel.getInstance().getLoggedUser() != null) {
    					PublishActivityCall call = new PublishActivityCall(ActivityEnum.PLAYING_AGAINST_HUMAN, SettingsModel.getInstance().getLoggedUser().getUsername(), SettingsModel.getInstance().getLoggedUser().getName(), "");			    		
    					SettingsModel.EXECUTOR_SERVICE.execute(call);
    					SettingsModel.getInstance().setLastActivity(ActivityEnum.PLAYING_AGAINST_HUMAN);
    				}*/
    	    		
    	    		activity.playNewGame(NewGame.HUMAN, false);
    	    	} else if(item == 1) {
    	    		Game.COMPUTER_TIME = 1;
    	    		
    	    		/*if(SettingsModel.getInstance().getLoggedUser() != null) {
    					PublishActivityCall call = new PublishActivityCall(ActivityEnum.PLAYING_AGAINST_ANDROID_NORMAL, SettingsModel.getInstance().getLoggedUser().getUsername(), SettingsModel.getInstance().getLoggedUser().getName(), "");			    		
    					SettingsModel.EXECUTOR_SERVICE.execute(call);
    					SettingsModel.getInstance().setLastActivity(ActivityEnum.PLAYING_AGAINST_ANDROID_NORMAL);
    				}*/
    	    		
    	    		activity.playNewGame(NewGame.COMPUTER, false);
    	    	} else if(item == 2) {
    	    		Game.COMPUTER_TIME = 5000;
    	    		
    	    		/*if(SettingsModel.getInstance().getLoggedUser() != null) {
    					PublishActivityCall call = new PublishActivityCall(ActivityEnum.PLAYING_AGAINST_ANDROID_NORMAL, SettingsModel.getInstance().getLoggedUser().getUsername(), SettingsModel.getInstance().getLoggedUser().getName(), "");			    		
    					SettingsModel.EXECUTOR_SERVICE.execute(call);
    					SettingsModel.getInstance().setLastActivity(ActivityEnum.PLAYING_AGAINST_ANDROID_NORMAL);
    				}*/
    	    		
    	    		activity.playNewGame(NewGame.COMPUTER, false);
    	    	} else if(item == 3) {
    	    		if(BluetoothPlugin.getInstance().getmBluetoothAdapter() != null) {
    	    			BluetoothPlugin.getInstance().setFen(null);
    	    			BluetoothPlugin.getInstance().setHost(true);
    	    			BluetoothPlugin.getInstance().start();
    	    		} else {
        	    		Intent intent3 = new Intent();
        	        	intent3.setClass(SahModel.context, StartPositionActivity.class);
        	        	SahModel.context.startActivity(intent3);  
        	        	
        	        	/*if(SettingsModel.getInstance().getLoggedUser() != null) {
        	        		PublishActivityCall call = new PublishActivityCall(ActivityEnum.CREATING_START_POSITION, SettingsModel.getInstance().getLoggedUser().getUsername(), SettingsModel.getInstance().getLoggedUser().getName(), "");			    		
        	        		SettingsModel.EXECUTOR_SERVICE.execute(call);
        	        		SettingsModel.getInstance().setLastActivity(ActivityEnum.CREATING_START_POSITION);
        	        	}*/
    	    		}
    	    	} else if(item == 4) {
    	    		BluetoothPlugin.getInstance().setFen(null);
    	    		BluetoothPlugin.getInstance().setHost(false);
    	    		BluetoothPlugin.getInstance().start();
    	    	} else if(item == 5) {
    	    		Intent intent3 = new Intent();
    	        	intent3.setClass(SahModel.context, StartPositionActivity.class);
    	        	SahModel.context.startActivity(intent3);
    	        	
    	        	/*if(SettingsModel.getInstance().getLoggedUser() != null) {
    	        		PublishActivityCall call = new PublishActivityCall(ActivityEnum.CREATING_START_POSITION, SettingsModel.getInstance().getLoggedUser().getUsername(), SettingsModel.getInstance().getLoggedUser().getName(), "");			    		
    	        		SettingsModel.EXECUTOR_SERVICE.execute(call);
    	        		SettingsModel.getInstance().setLastActivity(ActivityEnum.CREATING_START_POSITION);
    	        	}*/
    	    	}
    	    }
    	});
    	AlertDialog alert = builder.create();
    	alert.show();
    }
    
    public static void chooseNewGameType(final SahMatActivity activity, String fen, String white, String black, ArrayList<Move> moves) {
    	AlertDialog alert = chooseNewGameType(activity, fen, white, black, true);
    	
    	alert.setTitle(activity.getResources().getString(R.string.continue_game_against) + ":");
    	    	
    	Game game = SahModel.getInstance().getGame();
    	
    	game.setMoves(moves);
    }
    
    public static AlertDialog chooseNewGameType(final SahMatActivity activity, final String fen, String white, String black, final boolean goToEnd) {
    	
    	SahModel.getInstance().reset(fen);
    	
    	Game game = SahModel.getInstance().getGame();
    	
    	game.getPlayer1().setName(white);
    	game.getPlayer2().setName(black);
    	
    	BluetoothPlugin.getInstance().stop();
    	
    	final ArrayList<DeviceItem> gameOptions = new ArrayList<DeviceItem>();
    	
    	gameOptions.add(new DeviceItem(activity.getResources().getString(R.string.human_device), R.drawable.human));
    	gameOptions.add(new DeviceItem(activity.getResources().getString(R.string.computer_device_medium), R.drawable.android));
    	gameOptions.add(new DeviceItem(activity.getResources().getString(R.string.computer_device_hard), R.drawable.android));
    	
    	if(BluetoothPlugin.getInstance().getmBluetoothAdapter() != null && !goToEnd) {
    		gameOptions.add(new DeviceItem(activity.getResources().getString(R.string.bluetooth_host), R.drawable.bluetooth));
    	}

		ListAdapter adapter = new ArrayAdapter<DeviceItem>(activity, android.R.layout.select_dialog_item, android.R.id.text1, gameOptions){
	        public View getView(int position, View convertView, ViewGroup parent) {
	            View v = super.getView(position, convertView, parent);
	            TextView tv = (TextView)v.findViewById(android.R.id.text1);

	            tv.setCompoundDrawablesWithIntrinsicBounds(gameOptions.get(position).icon, 0, 0, 0);
	            tv.setTextSize(18);

	            int dp5 = (int) (5 * activity.getResources().getDisplayMetrics().density + 0.5f);
	            tv.setCompoundDrawablePadding(dp5);

	            return v;
	        }
		};

    	AlertDialog.Builder builder = new AlertDialog.Builder(activity);
    	builder.setTitle(activity.getResources().getString(R.string.play_new_game_against) + ":");
    	builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
    	    public void onClick(DialogInterface dialog, int item) {   	    	
    	    	if(item == 0) {
    	    		activity.playNewGame(NewGame.HUMAN, goToEnd);
    	    	} else if(item == 1) {
    	    		Game.COMPUTER_TIME = 1;
    	    		activity.playNewGame(NewGame.COMPUTER, goToEnd);
    	    	} else if(item == 2) {
    	    		Game.COMPUTER_TIME = 5000;
    	    		activity.playNewGame(NewGame.COMPUTER, goToEnd);
    	    	} else if(item == 3) {
	    			BluetoothPlugin.getInstance().setFen(fen);
    	    		BluetoothPlugin.getInstance().setHost(true);
	    			BluetoothPlugin.getInstance().start();
    	    	}
    	    }
    	});
    	AlertDialog alert = builder.create();
    	alert.show();
    	
    	return alert;
    }

    
    public static void chooseLanguage(final SettingsActivity activity) {
    	final ArrayList<DeviceItem> languages = new ArrayList<DeviceItem>();
    	
    	languages.add(new DeviceItem(activity.getResources().getString(R.string.bosnian_text), R.drawable.bih));
    	languages.add(new DeviceItem(activity.getResources().getString(R.string.english_text), R.drawable.eng));

		ListAdapter adapter = new ArrayAdapter<DeviceItem>(activity, android.R.layout.select_dialog_item, android.R.id.text1, languages){
	        public View getView(int position, View convertView, ViewGroup parent) {
	            View v = super.getView(position, convertView, parent);
	            TextView tv = (TextView)v.findViewById(android.R.id.text1);

	            tv.setCompoundDrawablesWithIntrinsicBounds(languages.get(position).icon, 0, 0, 0);
	            tv.setTextSize(18);

	            int dp5 = (int) (5 * activity.getResources().getDisplayMetrics().density + 0.5f);
	            tv.setCompoundDrawablePadding(dp5);

	            return v;
	        }
		};

    	AlertDialog.Builder builder = new AlertDialog.Builder(activity);
    	builder.setTitle(activity.getResources().getString(R.string.choose_language) + ":");
    	builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
    	    public void onClick(DialogInterface dialog, int item) {
    	    	if(item == 0) {
    	    		SettingsModel.getInstance().setLanguage("ba");
    	    	} else if(item == 1) {
    	    		SettingsModel.getInstance().setLanguage("ee");
    	    	}
    	    	
    	    	setLanguage(SettingsModel.getInstance().getLanguage(), activity);
    	    	activity.finish();
    	    	SahModel.context.finish();
	        	Intent intent = new Intent();
	        	intent.setClass(SahModel.context, SahMatActivity.class);
	        	SahModel.context.startActivity(intent);
    	    }
    	});
    	AlertDialog alert = builder.create();
    	alert.show();
    }
    
    public static void setLanguage(String lang, Context context) {
    	Resources res = context.getResources();
        DisplayMetrics dm = res.getDisplayMetrics();
        android.content.res.Configuration conf = res.getConfiguration();
        conf.locale = new Locale(lang.toLowerCase());
        res.updateConfiguration(conf, dm);
    }
    
    public static byte[] objectToBytes(Object yourObject) {
    	ByteArrayOutputStream bos = new ByteArrayOutputStream();
    	ObjectOutput out;
		try {
			out = new ObjectOutputStream(bos);
	    	out.writeObject(yourObject);
	    	
	    	out.close();
		} catch (IOException e) {
			return null;
		}   
    	
		return bos.toByteArray();
    }
    
    public static void playCheck() {
    	if(SettingsModel.getInstance().isPlaySound()) {
	    	try {
	    		if(mediaPlayerCheck == null) {
		    		mediaPlayerCheck = MediaPlayer.create(SahModel.context, R.raw.check);
		    		mediaPlayerCheck.setVolume(0.F, 0.1F);
	    		}
	    		
	    		mediaPlayerCheck.start();
	    	} catch (Exception e) { }
    	}
    }
    
    public static void playCheckMate() {
    	if(SettingsModel.getInstance().isPlaySound()) {
		    try {	
		    	if(mediaPlayerCheckmate == null) {
		    		mediaPlayerCheckmate = MediaPlayer.create(SahModel.context, R.raw.checkmate);
		    		mediaPlayerCheckmate.setVolume(0.1F, 0.1F);
		    	}
		    	mediaPlayerCheckmate.start();
    		} catch (Exception e) { }
    	}
    }
    
    public static void playPlayer1Wins() {
    	if(SettingsModel.getInstance().isPlaySound()) {
    		try {
	    		if(mediaPlayerPlayer1Wins == null) {
	    			mediaPlayerPlayer1Wins = MediaPlayer.create(SahModel.context, R.raw.playeronewins);
	    			mediaPlayerPlayer1Wins.setVolume(0.1F, 0.1F);
	    		}
	    		mediaPlayerPlayer1Wins.start();
    		} catch (Exception e) { }
    	}
    }
    
    public static void playPlayer2Wins() {
    	if(SettingsModel.getInstance().isPlaySound()) {
	    	try {
	    		if(mediaPlayerPlayer2Wins == null) {
	    			mediaPlayerPlayer2Wins = MediaPlayer.create(SahModel.context, R.raw.playertwowins);
	    			mediaPlayerPlayer2Wins.setVolume(0.1F, 0.1F);
	    		}
	    		mediaPlayerPlayer2Wins.start();
	    	} catch (Exception e) { }
    	}
    }
    
    public static void playMove() {
    	if(SettingsModel.getInstance().isPlaySound()) {
	    	try {
	    		if(mediaPlayerMove == null) {
	    			mediaPlayerMove = MediaPlayer.create(SahModel.context, R.raw.move);
	    			mediaPlayerMove.setVolume(0.5F, 0.5F);
	    		}
	    		mediaPlayerMove.start();
	    	} catch (Exception e) { }
    	}
    }
    
    public static void playCastle() {
    	if(SettingsModel.getInstance().isPlaySound()) {
	    	try {
	    		if(mediaPlayerCastle == null) {
	    			mediaPlayerCastle = MediaPlayer.create(SahModel.context, R.raw.castle);
	    			mediaPlayerCastle.setVolume(0.5F, 0.5F);
	    		}
	    		mediaPlayerCastle.start();
	    	} catch (Exception e) { }
    	}
    }

    public static void playCapture() {
    	if(SettingsModel.getInstance().isPlaySound()) {
	    	try {
	    		if(mediaPlayerCapture == null) {
	    			mediaPlayerCapture = MediaPlayer.create(SahModel.context, R.raw.capture);
	    			mediaPlayerCapture.setVolume(0.2F, 0.2F);
	    		}
	    		mediaPlayerCapture.start();
	    	} catch (Exception e) { }
    	}
    }

    public static Object bytesToObject(byte[] bytes) {
    	ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
    	
    	ObjectInput in;
		try {
			in = new ObjectInputStream(bis);
			
			Object o = in.readObject();

	    	bis.close();
	    	in.close();
	    	
	    	return o;
		} catch (StreamCorruptedException e) {
		} catch (IOException e) {
		} catch (ClassNotFoundException e) {
		}

    	return null;
    }
    
    public static int getOrientation() {
    	/*Display display = SahModel.context.getWindowManager().getDefaultDisplay();
    	
    	int orientation = 0;
    	
    	if (android.os.Build.VERSION.SDK_INT > 7){
    		try {
    			Method getRot = display.getClass().getMethod("getRotation");
    			return (Integer)getRot.invoke(display);
    		} catch(Exception e) {
    		}
    		return display.getOrientation();
    	} else {
    		orientation = display.getOrientation();
		}*/
    	
    	if(SahModel.context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) return 1; else return 0; 
    	
    	//return orientation;
    }
    
    public static int geOldtOrientation() {
    	Display display = SahModel.context.getWindowManager().getDefaultDisplay();
    	
    	int orientation = 0;
    	
    	if (android.os.Build.VERSION.SDK_INT > 7){
    		try {
    			Method getRot = display.getClass().getMethod("getRotation");
    			return (Integer)getRot.invoke(display);
    		} catch(Exception e) {
    		}
    		return display.getOrientation();
    	} else {
    		orientation = display.getOrientation();
		}
    	
    	return orientation;
    }
    
    public static int convertDPItoPixels(int dpi) {
    	return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpi, SahModel.context.getResources().getDisplayMetrics());
    }
    
    /*public static String objectToString(Serializable object) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            new ObjectOutputStream(out).writeObject(object);
            byte[] data = out.toByteArray();
            out.close();

            out = new ByteArrayOutputStream();
            Base64OutputStream b64 = new Base64OutputStream(out, Base64.DEFAULT);
            b64.write(data);
            b64.close();
            out.close();

            return new String(out.toByteArray());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Object stringToObject(String encodedObject) {
        try {
            return new ObjectInputStream(new Base64InputStream(new ByteArrayInputStream(encodedObject.getBytes()), Base64.DEFAULT)).readObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }*/
}
